import axios from 'axios';
import qs from 'qs';
import { message } from 'antd';
let loading = document.getElementById('ajaxLoading');

const service = axios.create({
  baseURL: 'https://www.easy-mock.com/mock/5b8f76a2788cf57c631d8d35/bike',
  // baseURL: 'https://www.easy-mock.com/mock/5a7278e28d0c633b9c4adbd7/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  },
  transformRequest: [
    data => {
      return qs.stringify(data);
    }
  ]
});

// asiox 请求拦截
service.interceptors.request.use(
  config => {
    loading.style.display = 'block';
    return config;
  },
  error => {
    console.log(error);
    Promise.reject(error);
  }
);

// 响应拦截
service.interceptors.response.use(
  response => {
    loading.style.display = 'none';
    let res = response.data;
    if (res.code === 0) {
      return res;
    } else {
      message.error(res.message);
    }
  },
  error => {
    console.log('err' + error);
    message.error(error.message);
    return Promise.reject(error);
  }
);

export default service;
